package br.edu.ufcg.msnlab2.grupo02.booleSimpson;

import br.edu.ufcg.msnlab2.misc.Function;
import br.edu.ufcg.msnlab2.misc.NumericalIntegrationMethod;

/**
 * 
 * @author Alexandro S. Soares - alexandrodss@gmail.com
 * @author Raphael Rodrigues M. Mattos - mattos.raphael@gmail.com
 */
public class BoolesRule implements NumericalIntegrationMethod {

	private static final int DEFAULT_NUMBER_OF_SUBINTERVALS = 2;
	private final int numberOfSubintervals;

	public BoolesRule() {
		this(DEFAULT_NUMBER_OF_SUBINTERVALS);
	}

	public BoolesRule( int numberOfSubintervals ) {
		this.numberOfSubintervals = numberOfSubintervals;
	}

	@Override
	public double integrate( Function f, double a, double b ) {

		double h = ( b - a ) / (4 * numberOfSubintervals);
		double x0 = a;
		double x1 = x0 + (1*numberOfSubintervals)*h;
		double x2 = x0 + (2*numberOfSubintervals)*h;
		double x3 = x0 + (3*numberOfSubintervals)*h;
		double x4 = b;
		return ((2*numberOfSubintervals * h)/45) * ( (7 * f.evaluate( x0 )) + (32 * f.evaluate( x1 )) + (12 * f.evaluate( x2 )) + (32 * f.evaluate( x3 )) + (7 * f.evaluate( x4 )));

	}

}
